home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor.zip / CHAP13.TXT < prev    next >
Text File  |  1987-07-04  |  6KB  |  190 lines

  1.                  Chapter 13 - Character and Bit Manipulation
  2.  
  3.  
  4.                             UPPER AND LOWER CASE
  5.  
  6.              Load  and display the program UPLOW.C for an example of
  7.         a  program that does lots of character  manipulation.   More
  8.         specifically,  it changes the case of alphabetic  characters
  9.         around.   It illustrates the use of four functions that have
  10.         to  do with case.   It should be no problem for you to study
  11.         this program on your own and understand how it  works.   The
  12.         four functions on display in this program are all within the
  13.         user written function, "mix_up_the_chars".  Compile and  run
  14.         the  program  with  the  file  of  your  choice.   The  four
  15.         functions are;
  16.  
  17.              isupper();     Is the character upper case?
  18.              islower();     Is the character lower case?
  19.              toupper();     Make the character upper case.
  20.              tolower();     Make the character lower case.
  21.  
  22.              Many  more Classification and Conversion  routines  are
  23.         listed on pages 10 and 11 of your Turbo C Reference Guide.
  24.  
  25.                         CLASSIFICATION OF CHARACTERS
  26.  
  27.              Load  and display the next program, CHARCLAS.C  for  an
  28.         example of character counting.  We have repeatedly used  the
  29.         backslash  n character representing a new line.   These  are
  30.         called escape sequences, and some of the more commonly  used
  31.         are defined in the following table;
  32.  
  33.              \n             Newline
  34.              \t             Tab
  35.              \b             Backspace
  36.              \"             Double quote
  37.              \\             Backslash
  38.              \0             NULL (zero)
  39.  
  40.              A complete list of escape sequences available with your
  41.         Turbo  C  compiler are listed on page 201 of  your  Turbo  C
  42.         Reference Manual.
  43.  
  44.              By  preceding  each of the above  characters  with  the
  45.         backslash character, the character can be included in a line
  46.         of text for display,  or printing.   In the same way that it
  47.         is  perfectly  all right to use the letter "n" in a line  of
  48.         text as a part of someone's name, and as an end-of-line, the
  49.         other  characters can be used as parts of text or for  their
  50.         particular functions.
  51.  
  52.              The program on your screen uses the functions that  can
  53.         determine   the  class  of  a  character,   and  counts  the
  54.         characters  in  each class.   The number of  each  class  is
  55.  
  56.  
  57.                                    Page 94
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                  Chapter 13 - Character and Bit Manipulation
  68.  
  69.  
  70.         displayed  along with the line itself.   The three functions
  71.         are as follows;
  72.  
  73.              isalpha();     Is the character alphabetic?
  74.              isdigit();     Is the character a numeral?
  75.              isspace();     Is the character any of, \n, \t,
  76.                               or blank?
  77.  
  78.              As  noted above, many more Classification Routines  are
  79.         listed on page 10 of your Turbo C Reference Guide.
  80.  
  81.              This program should be simple for you to find your  way
  82.         through  so no explanation will be given.   It was necessary
  83.         to give an example with these functions used.   Compile  and
  84.         run this program with any file you choose.
  85.  
  86.                            THE LOGICAL FUNCTIONS
  87.  
  88.              Load and display the program BITOPS.C. The functions in
  89.         this  group of functions are used to do bitwise  operations,
  90.         meaning  that  the operations are performed on the  bits  as
  91.         though they were individual bits.   No carry from bit to bit
  92.         is performed as would be done with a binary addition.   Even
  93.         though  the operations are performed on a single bit  basis,
  94.         an entire byte or integer variable can be operated on in one
  95.         instruction.   The operators and the operations they perform
  96.         are given in the following table;
  97.  
  98.              &    Logical AND, if both bits are 1, the result is 1.
  99.              |    Logical OR, if either bit is one, the result is 1.
  100.              ^    Logical XOR,  (exclusive OR),  if one and only one
  101.                     bit is 1, the result is 1.
  102.              ~    Logical invert,  if the bit is 1, the result is 0,
  103.                     and if the bit is 0, the result is 1.
  104.  
  105.              The  example  program  uses  several  fields  that  are
  106.         combined  in each of the ways given above.   The data is  in
  107.         hexadecimal  format.   It  will be assumed that you  already
  108.         know hexadecimal format if you need to use these operations.
  109.         If  you  don't,  you  will need to study  it  on  your  own.
  110.         Teaching  the  hexadecimal format of numbers is  beyond  the
  111.         scope of this tutorial.
  112.  
  113.              Run the program and observe the output.
  114.  
  115.                            THE SHIFT INSTRUCTIONS
  116.  
  117.              The  last two operations to be covered in this  chapter
  118.         are  the left shift and the right shift instructions.   Load
  119.         the example program SHIFTER.C for an example using these two
  120.  
  121.  
  122.  
  123.                                    Page 95
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                  Chapter 13 - Character and Bit Manipulation
  134.  
  135.  
  136.         instructions.    The   two  operations  use  the   following
  137.         operators;
  138.  
  139.              << n      Left shift n places.
  140.              >> n      Right shift n places.
  141.  
  142.              Once again the operations are carried out and displayed
  143.         using the hexadecimal format.   The program should be simple
  144.         for you to understand on your own, there is no tricky code.
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.                                    Page 96
  190.